package inventoryservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type InventoryAssetReceiptAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInventoryAssetReceiptAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InventoryAssetReceiptAddLogic {
	return &InventoryAssetReceiptAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// InventoryAssetReceiptAdd 创建资产入库表
func (l *InventoryAssetReceiptAddLogic) InventoryAssetReceiptAdd(in *property_service.InventoryAssetReceipt) (*property_service.ResponseId, error) {
	// 获取资产入库表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).InventoryAssetReceipt
	// 创建一个新的资产入库表模型对象，不包含ID、UpdatedAt、CreatedAt字段
	mod := &model.InventoryAssetReceipt{
		ReceiptCode:            in.ReceiptCode,                          // 入库单号
		ReceiptDate:            BeanUtils.GetDateFormat(in.ReceiptDate), // 入库日期
		EquipmentSource:        in.EquipmentSource,                      // 设备来源
		Receiver:               in.Receiver,                             // 验收人
		Warehouse:              in.Warehouse,                            // 库房
		Supplier:               in.Supplier,                             // 供应商
		SupplierID:             in.SupplierID,                           //供应商id
		Purchaser:              in.Purchaser,                            // 采购员
		ContractNumber:         in.ContractNumber,                       // 合同编号
		InvoiceNumber:          in.InvoiceNumber,                        // 发票号码
		InvoiceDate:            BeanUtils.GetDateFormat(in.InvoiceDate), // 发票日期
		DepreciationMethod:     in.DepreciationMethod,                   // 折旧方法
		DepreciationMethodCode: in.DepreciationMethodCode,               // 折旧方法Code
		AssetUse:               in.AssetUse,                             // 资产用途
		ReceiptMethod:          in.ReceiptMethod,                        // 入库方式
		InstallationNumber:     in.InstallationNumber,                   // 安装单号
		StatusID:               in.StatusID,                             // 状态ID
		StockID:                in.StockID,                              // 仓库ID
		TenantID:               in.TenantID,                             // 租户ID
		AssetUseCode:           in.AssetUseCode,                         //资产用途Code
		ReceiptMethodCode:      in.ReceiptMethodCode,                    //入库方式Code
		PurchaseID:             in.PurchaseID,                           // 采购申请单ID
		PurchaseCode:           in.PurchaseCode,                         // 采购編碼
		CreatedAtuser:          in.CreatedAtuser,                        // 创建用户
	}

	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		res, err := result.Err(err).Ed()
		return l.buildResponse(mod.ID, res, err)
	}
	// 创建成功，返回成功响应
	res, err := result.Suc().AddEd()
	return l.buildResponse(mod.ID, res, err)
}

// 在 InventoryAssetReceiptAddLogic 结构体中添加辅助方法
func (l *InventoryAssetReceiptAddLogic) buildResponse(id int64, res *property_service.Response, err error) (*property_service.ResponseId, error) {
	return &property_service.ResponseId{
		Id:      id,
		Code:    res.Code,
		Message: res.Message,
	}, err
}
